package com.jg.servlet;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.jg.entity.EmployeeProfiles;
import com.jg.service.EmployeeProfilesService;
import com.jg.service.impl.EmployeeProfilesServiceImpl;
import com.jg.util.DateUtil;
import com.jg.util.FileUtil;
import com.jg.util.Page;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 86130
 * @version 1.0
 * @description: TODO
 * @date 2025/4/18 09:53
 */
@WebServlet("/empsys")
@MultipartConfig(maxFileSize = 1024 * 1024 * 2)
public class EmployeeServlet extends HttpServlet {
    EmployeeProfilesService employeeService = new EmployeeProfilesServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        //允许所有来源访问
        resp.addHeader("Access-Control-Allow-Origin", "*");
        //允许请求的方法
        resp.addHeader("Access-Control-Allow-Methods", "POST, GET");
        String method = req.getParameter("method") == null ? "list" : req.getParameter("method");
        switch (method) {
            case "list":
                list(req, resp);
                break;
            case "edit":
                edit(req, resp);
                break;
            case "detail":
                detail(req, resp);
                break;
            case "delete":
                delete(req, resp);
            default:
                break;
        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String empId = req.getParameter("empId");
        employeeService.deleteEmployee(Integer.parseInt(empId));
        Map<String, String> map = new HashMap<>();
        map.put("msg", "OK");
        String jsonString = JSON.toJSONString(map);
        resp.getWriter().write(jsonString);
    }

    private void detail(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String empId = req.getParameter("empId");
        EmployeeProfiles employee = employeeService.getEmployeeById(Integer.parseInt(empId));
        String jsonString = JSON.toJSONString(employee, "yyyy-MM-dd", JSONWriter.Feature.WriteMapNullValue);
        resp.getWriter().write(jsonString);
    }

    private void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String empId = req.getParameter("empId");
        String empName = req.getParameter("empName");
        String deptId = req.getParameter("departmentId");
        String position = req.getParameter("position");
        String startTime = req.getParameter("hireDate");
        String phone = req.getParameter("phone");
        Part photo = req.getPart("photo");
        String photoUrl = "";
        if (photo != null) {
            photoUrl = FileUtil.getImgUrl(req, photo);
        }

//        String photoUrl = FileUtil.getImgUrl(req, photo);
        EmployeeProfiles employee = new EmployeeProfiles();
        if (empId == null) {
            employee.setId(0);
        } else {
            employee.setId(Integer.parseInt(empId));
        }
        employee.getName();
        employee.getDepartment();
        employee.getPosition();
        employee.getHireDate();
        employee.getLeaveDate();
        employee.getStatus();
        int result = employeeService.edit(employee);
        Map<String, String> map = new HashMap<>();
        if (result > 0) {
            map.put("code", "200");
            map.put("msg", "员工信息修改成功");
        } else {
            map.put("code", "500");
            map.put("msg", "员工信息修改失败");
        }
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //当前页码
        String currentPage = req.getParameter("currentPage");
        //搜索用户名
        String empName = req.getParameter("empName");
        if (currentPage == null || currentPage.equals("")) {
            currentPage = "1";
        }
        Page<EmployeeProfiles> page = new Page<EmployeeProfiles>();
        page.setCurrentPage(Integer.parseInt(currentPage));
        employeeService.getEmployeeList(page, empName);
        String jsonString = JSON.toJSONString(page, "yyyy-MM-dd", JSONWriter.Feature.WriteMapNullValue);
        resp.getWriter().write(jsonString);
    }
}
